<template>
  <BaseMainSlot>
    <template v-slot:searchbox>
      <div class="seach-box">
        <div class="select-box-content">
          <div class="select-box-content-name">备件类型名称：</div>
          <el-input
            v-model="sparePartTypeName"
            clearable
            placeholder="请输入备件类型名称"
            @change="queryPageList()"
          />
        </div>
      </div>
    </template>
    <template v-slot:btnsBar>
      <ActionBar
        :add-active="addActive"
        :del-active="delActive"
        :page-no="161"
        @add="add"
        @del="del"
      />
    </template>
    <template v-slot:content>
      <e-table
        ref="tableData"
        rowKey="sparePartTypeCode"
        :table-data="tableData"
        :table-columns="tableLabel"
        :page="currentPage"
        :size="pageSize"
        :total="totalSize"
        :loading="loading"
        :table-option="tableOption"
        @sizeChange="changePageSize"
        @pageChange="changeCurrentPage"
        @handleSelectionChange="handleSelectionChange"
        @tableUpdatePlus="mixTableUpdatePlus"
      >
        <template
          slot="operate"
          v-if="scoped.row.sparePartTypeCode"
          slot-scope="scoped"
        >
          <span
            v-if="judgePermission(161,'edit')"
            class="table-btns-box"
            @click="edit(scoped.row)"
          >
            <i class="el-icon-edit-outline"></i>
            <span>修改</span>
          </span>

          <span
            v-if="judgePermission(161,'deleteone')"
            class="table-btns-box table_del_btn"
            @click="delOne(scoped.row)"
          >
            <i class="el-icon-delete table_del_btn"></i>
            <span>删除</span>
          </span>
        </template>
      </e-table>
      <AddEditDialog
        ref="addEditDialog"
        :now-chance-stau="nowChanceStau"
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showAddEditDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import sparePartTypeApi from "@/api/spare-part-manage/spare-part-type";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new.vue";
import AddEditDialog from "./components/add-edit-dialog.vue";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from "vuex";
export default {
  name: "SparePartType",
  components: { ActionBar, BaseMainSlot, AddEditDialog },
  mixins: [mymixin],
  data() {
    return {
      // 控制弹出框显示隐藏
      showAddEditDialog: false,
      // 查询条件：备件名称
      sparePartTypeName: "",
      checkedIdList: [],
      tableLabel: [
        {
          label: "类型名称",
          param: "sparePartTypeName",
          show: true,
          notSearch: true,
        },
        {
          label: "备注",
          param: "description",
          show: true,
          notSearch: true,
        },
      ],
      // 表单事件
      tableOption: {
        label: "操作",
        options: [
          {
            label: "操作",
            slotName: "operate",
          },
        ],
      },
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission",
    }),
  },
  watch: {},
  created() {
    this.queryPageList();
  },
  mounted() {},
  methods: {
    queryPageList(e) {
      this.loading = false;
      // 获取数据
      let param = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          sparePartTypeName: this.sparePartTypeName || e?.sparePartTypeName,
        },
      };
      sparePartTypeApi.queryPageList(param).then((res) => {
        this.loading = false;
        if (res.code === 1) {
          let data = res.data;
          this.totalSize = data.totalElements;
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },

    handleEdit(row) {
      if (row.sparePartTypeCode) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.addActive = true;
        this.editActive = true;
      } else {
        this.editObject = {};
        this.addActive = true;
        this.editActive = false;
      }
    },
    selectAll(list) {
      let notvalue = true;
      list.forEach((item) => {
        if (!item.sparePartTypeCode) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    select(list) {
      let notvalue = true;
      list.forEach((item) => {
        if (!item.sparePartTypeCode) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    handleSelectionChange(list) {
      this.checkedIdList = [];
      let notvalue = true;
      let emptyList = [];
      this.checkedIdList = list.map((item) => {
        if (item.sparePartTypeCode) {
          return item.sparePartTypeCode;
        } else {
          notvalue = false;
          emptyList.push(item);
          return "";
        }
      });
      if (notvalue === false) {
        // this.$message.warning("不可选择空数据！");
        this.toggleNoSelection(emptyList);
        return;
      }
      if (this.checkedIdList.length >= 1) this.delActive = true;
      else this.delActive = false;
    },
    // 单个删除
    delOne(data) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass",
      })
        .then(() => {
          var form = new FormData();
          form.append("sparePartTypeCodeList", data.sparePartTypeCode);
          sparePartTypeApi.delList(form).then((res) => {
            if (res.code === 1) {
              this.$message.success(res.msg);
              this.queryPageList();
              this.resetActionBar();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 表格删除
    delmessage() {
      var form = new FormData();
      this.checkedIdList.forEach((a) => {
        form.append("sparePartTypeCodeList", a);
      });
      sparePartTypeApi.delList(form).then((res) => {
        if (res.code === 1) {
          this.$message.success(res.msg);
          this.queryPageList();
          this.resetActionBar();
        }
      });
    },
    // 添加
    add() {
      this.nowChanceStau = "add";
      this.titleText = "添加";
      this.addActive = false;
      this.delActive = false;
      this.showAddEditDialog = true;
    },
    // 修改
    edit(row) {
      if (row.sparePartTypeCode) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = "edit";
        this.titleText = "修改";
        this.addActive = false;
        this.delActive = false;
        this.showAddEditDialog = true;
      } else {
        this.editObject = {};
      }
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-collapse-item__header {
  height: 0px;
}
::v-deep .el-collapse-item__content {
  padding-bottom: 10px;
}
</style>
